| Conditions | 1 |
| Paths | 4 |
| Total Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | 'use strict'; |
||
| 3 | const driver = options => { |
||
| 4 | const Wappalyzer = require('./wappalyzer'); |
||
| 5 | const request = require('request'); |
||
| 6 | const url = require('url'); |
||
| 7 | const fs = require('fs'); |
||
| 8 | const Browser = require('zombie'); |
||
| 9 | |||
| 10 | const json = JSON.parse(fs.readFileSync(__dirname + '/apps.json')); |
||
| 11 | |||
| 12 | return { |
||
| 13 | analyze: pageUrl => { |
||
| 14 | const origPageUrl = url.parse(pageUrl); |
||
| 15 | const analyzedPageUrls = []; |
||
| 16 | const apps = []; |
||
| 17 | |||
| 18 | const wappalyzer = new Wappalyzer(); |
||
| 19 | |||
| 20 | wappalyzer.apps = json.apps; |
||
| 21 | wappalyzer.categories = json.categories; |
||
| 22 | |||
| 23 | wappalyzer.driver.log = (message, source, type) => { |
||
| 24 | if ( Boolean(options.debug) ) { |
||
| 25 | console.log('[wappalyzer ' + type + ']', '[' + source + ']', message); |
||
| 26 | } |
||
| 27 | }; |
||
| 28 | |||
| 29 | wappalyzer.driver.displayApps = detected => { |
||
| 30 | Object.keys(detected).forEach(appName => { |
||
| 31 | const app = detected[appName]; |
||
| 32 | |||
| 33 | var categories = []; |
||
| 34 | |||
| 35 | app.props.cats.forEach(id => { |
||
| 36 | var category = {}; |
||
| 37 | |||
| 38 | category[id] = wappalyzer.categories[id].name; |
||
| 39 | |||
| 40 | categories.push(category) |
||
| 41 | }); |
||
| 42 | |||
| 43 | if ( !apps.some(detectedApp => detectedApp.name === app.name) ) { |
||
| 44 | apps.push({ |
||
| 45 | name: app.name, |
||
| 46 | confidence: app.confidenceTotal.toString(), |
||
| 47 | version: app.version, |
||
| 48 | icon: app.props.icon || 'default.svg', |
||
| 49 | website: app.props.website, |
||
| 50 | categories |
||
| 51 | }); |
||
| 52 | } |
||
| 53 | }); |
||
| 54 | }; |
||
| 55 | |||
| 56 | const browser = new Browser({ |
||
| 57 | userAgent: options.userAgent, |
||
| 58 | waitDuration: options.maxWait + 'ms', |
||
| 59 | }); |
||
| 60 | |||
| 61 | const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); |
||
| 62 | |||
| 63 | const fetch = (pageUrl, index, depth) => { |
||
| 64 | return new Promise(async (resolve, reject) => { |
||
| 65 | // Return when the URL is a duplicate or maxUrls has been reached |
||
| 66 | if ( analyzedPageUrls.indexOf(pageUrl.href) !== -1 || analyzedPageUrls.length >= options.maxUrls ) { |
||
| 67 | return resolve(); |
||
| 68 | } |
||
| 69 | |||
| 70 | analyzedPageUrls.push(pageUrl.href); |
||
| 71 | |||
| 72 | wappalyzer.log('depth: ' + depth + '; delay: ' + ( options.delay * index ) + 'ms; url: ' + pageUrl.href, 'driver'); |
||
| 73 | |||
| 74 | // Be nice |
||
| 75 | if ( options.delay ) { |
||
| 76 | await sleep(options.delay * index); |
||
| 77 | } |
||
| 78 | |||
| 79 | browser.visit(pageUrl.href, error => { |
||
| 80 | if ( !browser.resources['0'] || !browser.resources['0'].response ) { |
||
| 81 | wappalyzer.log('No response from server', 'browser', 'error'); |
||
| 82 | |||
| 83 | return resolve(); |
||
| 84 | } |
||
| 85 | |||
| 86 | browser.wait() |
||
| 87 | .catch(error => wappalyzer.log(error.message, 'browser')) |
||
| 88 | .finally(() => { |
||
| 89 | wappalyzer.driver.document = browser.document; |
||
| 90 | |||
| 91 | const headers = {}; |
||
| 92 | |||
| 93 | browser.resources['0'].response.headers._headers.forEach(header => { |
||
| 94 | if ( !headers[header[0]] ){ |
||
| 95 | headers[header[0]] = []; |
||
| 96 | } |
||
| 97 | |||
| 98 | headers[header[0]].push(header[1]); |
||
| 99 | }); |
||
| 100 | |||
| 147 |